package cuongnh.project.japanesediary.vocabulary.ui.editor.text.page.general.section;

import org.eclipse.core.databinding.DataBindingContext;
import org.eclipse.core.databinding.observable.ChangeEvent;
import org.eclipse.core.databinding.observable.IChangeListener;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.forms.SectionPart;
import org.eclipse.ui.forms.widgets.FormToolkit;
import org.eclipse.ui.forms.widgets.Section;

import cuongnh.project.japanesediary.vocabulary.common.CommonServices;
import cuongnh.project.japanesediary.vocabulary.model.Text;

public class TextGeneralInfoSection extends SectionPart implements
		ModifyListener, IChangeListener {
	private DataBindingContext m_bindingContext;
	private Text text;
	private CommonServices commonServices;
	private Composite container;

	// protected final DirtyFlag dirty = new DirtyFlag();

	/**
	 * Create the SectionPart.
	 * 
	 * @param parent
	 * @param toolkit
	 * @param style
	 * @wbp.parser.constructor
	 */
	public TextGeneralInfoSection(Composite parent, FormToolkit toolkit,
			int style) {
		super(parent, toolkit, style);
		createClient(getSection(), toolkit);
	}

	public TextGeneralInfoSection(Composite parent,
			Text text,
			CommonServices commonServices,
			FormToolkit toolkit, int style) {
		super(parent, toolkit, style);
		this.text = text;
		this.commonServices = commonServices;
		createClient(getSection(), toolkit);
	}

	/**
	 * Fill the section.
	 */
	private void createClient(Section section, FormToolkit toolkit) {
		createToolBar(section);
		section.setText("General information");
		container = toolkit.createComposite(section);

		section.setClient(container);
		container.setLayout(new GridLayout(1, false));
	}

	@Override
	public boolean isDirty() {
		// TODO Auto-generated method stub
		return super.isDirty();
	}

	@Override
	public void commit(boolean onSave) {
		System.out.println("Commit from Section part!!!");
		commonServices.getTextDao().merge(text);
		super.commit(onSave);
	}

	@Override
	public void markDirty() {
		super.markDirty();
	}

	@Override
	public void modifyText(ModifyEvent e) {
		super.markDirty();

	}

	@Override
	public void handleChange(ChangeEvent event) {
		super.markDirty();

	}

	private void createToolBar(Section section) {
		
	}

	public Composite getContainer() {
		return container;
	}
}
